package com.puao.controller;

import com.alibaba.fastjson.JSON;
import com.puao.bean.BallIn;
import com.puao.service.IAnalysisService;
import com.puao.util.InitNum;
import com.puao.util.ReadFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.Map.Entry;

@Controller
@RequestMapping("/analyse")
public class AnalysisController {

    @Autowired
    private IAnalysisService analysisService;

    @ResponseBody
    @RequestMapping("/analysis")
    public String analysis(Integer begin, Integer end) {
        List<Entry<String, List<String>>> listEntry = analysisService.getCount(begin, end);
        Map<String, Integer> map = analysisService.analysis(listEntry);
        Map<String, TreeSet<String>> res = combination(map);
        String json = JSON.toJSONString(res);
        return json;
    }

    /**
     * 组合关联数字
     *
     * @param map
     * @return
     */
    private Map<String, TreeSet<String>> combination(Map<String, Integer> map) {
        Map<String, TreeSet<String>> res = new HashMap<>();
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            String keySet = it.next().toString();
            String[] temp = keySet.split(",");
            if (Objects.isNull(temp)) {
                continue;
            }
            String key = temp[0];
            TreeSet<String> set = null;
            if (res.containsKey(key)) {
                set = res.get(key);
            } else {
                set = new TreeSet<>();
            }

            for (String str : temp) {
                if(str.length() < 3){
                    str = str.substring(0,1) + "0" + str.substring(1,2);
                }
                set.add(str);
            }
            res.put(key, set);
        }
        return res;
    }

    @ResponseBody
    @RequestMapping("red01")
    public List<Map<String,Object>> red01(Integer begin, Integer end){
        return analysisService.red01(begin, end);
    }
    @ResponseBody
    @RequestMapping("red02")
    public List<Map<String,Object>> red02(Integer begin, Integer end){
        return analysisService.red02(begin, end);
    }
    @ResponseBody
    @RequestMapping("red03")
    public List<Map<String,Object>> red03(Integer begin, Integer end){
        return analysisService.red03(begin, end);
    }
    @ResponseBody
    @RequestMapping("red04")
    public List<Map<String,Object>> red04(Integer begin, Integer end){
        return analysisService.red04(begin, end);
    }
    @ResponseBody
    @RequestMapping("red05")
    public List<Map<String,Object>> red05(Integer begin, Integer end){
        return analysisService.red05(begin, end);
    }
    @ResponseBody
    @RequestMapping("blue01")
    public List<Map<String,Object>> blue01(Integer begin, Integer end){
        return analysisService.blue01(begin, end);
    }
    @ResponseBody
    @RequestMapping("blue02")
    public List<Map<String,Object>> blue02(Integer begin, Integer end){
        return analysisService.blue02(begin, end);
    }

    @ResponseBody
    @RequestMapping("initBall")
    public List<BallIn> listNum(){
        List<Integer> list = Arrays.asList(3,6,13,16,18,21,24,25,26,33,35);
        Collections.sort(list);
        List<BallIn> balls = InitNum.generateNum(list);
        for(int i = 0 ; i < balls.size(); i++) {
            BallIn ball = balls.get(i);
            if(analysisService.isBeforeAppear(ball)){
                balls.remove(i);
            }
        }
        return balls;
    }

    @RequestMapping("/guess")
    @ResponseBody
    public Map<String,List<BallIn>> guess32(){
        String path ="D:\\guess\\32All.txt";
        List<String[]> inputs = ReadFile.getContent(path);
        List<List<String>> list = new ArrayList<>();
        for(int i = 0 ; i < inputs.size() ; i ++){
            String[] args = inputs.get(i);
            List<String> temp = new ArrayList<>();
            for(int j = 0 ; j < args.length ; j++){
               if(j == 5){
                   continue;
               }
                temp.add(args[j]);
            }
            list.add(temp);
        }
        List<BallIn> balls = InitNum.castToBallIn(list);
        List<BallIn> isIn = new ArrayList<>();
        for(int i = 0 ; i < balls.size(); i++) {
            BallIn ball = balls.get(i);
            if(analysisService.isBeforeAppear(ball)){
                isIn.add(ball);
                balls.remove(i);
            }
        }
        Map<String,List<BallIn>> map = new HashMap<>();
        map.put("notIn",balls);
        map.put("isIn",isIn);
        return  map;
    }

}
